import React, { useEffect, useState, useRef } from 'react'
import {
  Tabs,
  Input,
  List,
  Avatar,
  Row,
  Col,
  Space,
  AutoComplete,
  Divider,
  Select,
  Button,
  message
} from 'antd'
import {
  ProCard,
  ProDescriptions,
  ProTable,
  TableDropdown,
  ProFormText,
  ProForm,
  ProFormTextArea,
  ProFormRadio,
  ProFormDependency,
  ProFormSelect,
  FooterToolbar
} from '@ant-design/pro-components';
import { PlusOutlined } from '@ant-design/icons';
import {
  reqPatientList,
  reqDetal,
  reqUpdate,
  reqPulseUpdate,
  reqPulseDetail,
  reqPulseList,
  reqQuestionTermList,
  reqIllnessList
} from '@/services/basicConsultation';
import styles from './index.module.less'

const QuestionTerm = ({
  user,
  upDataInfo,
  readonly
}) => {
  const [formObj] = ProForm.useForm()

  const [data, setData] = useState([])

  async function init() {
    try {
      const res = await reqQuestionTermList({
        pageNum: 1,
        pageSize: 10,
        dept: "内科",
        // type: 1
      })

      console.log('病种病名列表信息 :>> ', res?.data);
      if (res?.resultCode == '00000000') {
        setData(res?.data?.result)
      } else {
        message.error(`病种病名列表信息出错！${res?.message}`)
      }
    } catch (error: any) {
      message.error('病种病名列表信息出错！', error)
      console.log('病种病名列表信息出错！ :>> ', error);
    }
  }


  useEffect(() => {
    if (user?.id && !user.basicInquiryJson) {
      console.log('user ==========:>> ', user);
      init()
    }
  }, [user])

  function jsonToarray(json: string) {
    const arr = JSON.parse(json);
    return arr.map((item: string) => ({
      label: item,
      value: item
    }))
  }


  async function handleAdd(values: object) {
    const basicInquiryJson = Object.keys(values).map((item: string) => {
      // const options = jsonToarray(item?.optionAnswer);
      let newItem = {
        // ...item,
        // optionAnswer: JSON.parse(item?.optionAnswer),
        // options,
        question: data[(item - 1)]?.question,
        value: null,
      };
      if (values[item]) {
        newItem.value = values[item]
      }
      return newItem
    })
    console.log('输入病史 :>> ', data, values, basicInquiryJson);
    // return;
    upDataInfo && upDataInfo({ basicInquiryJson: JSON.stringify(basicInquiryJson) })
  }

  const basicInquiryJson = JSON.parse(user.basicInquiryJson);


  return (
    !user.basicInquiryJson ?
      <ProForm
        form={formObj}
        readonly={readonly}
        onFinish={(values): Promise<any> => handleAdd(values)}
        submitter={{
          render: (_, dom) => <Space className={styles.tabPosition}>{dom}</Space>,
        }}
      >
        <Row gutter={[24, 12]}>
          {
            data.map((item: any, index: number) => {
              const options = jsonToarray(item?.optionAnswer);
              return (
                <Col span={24} key={index}>
                  <ProFormRadio.Group
                    name={item?.id}
                    fieldProps={{
                      optionType: 'button',
                      buttonStyle: 'solid',
                      className: styles.radioClass
                    }}
                    label={item?.question}
                    options={options}
                  />
                </Col>
              )
            })
          }
        </Row>
      </ProForm>
      :
      <Row gutter={[24, 12]}>
        {
          basicInquiryJson?.map((item: any, index: number) => (
            <Col span={24} key={index}>
              <div className={styles.question}>{item?.question}</div>
              <div>{item?.value}</div>
            </Col>
          ))
        }
      </Row>
  )
}


export default QuestionTerm